<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Facades\Utils\QiniuFileManager;

class Commodity extends Model
{
    protected $table = 'commodities';

    protected $fillable = [
      'id',
    	'c_name',
    	'c_number',
    	'c_integration',
    	'c_picture',
    	'ct_type_id'
    ];

    protected $appends = [
      'c_picture_url',
      'c_picture_arr'
    ];


    public function getCPictureUrlAttribute(){
      if(array_key_exists('c_picture',$this->attributes)){
        $tmpCPictureArr = json_decode($this->attributes['c_picture']);
        $tempArray = [];
        for($i = 0;$i<count($tmpCPictureArr);$i++){
          $tempArray[$i] = QiniuFileManager::getFileUrl($tmpCPictureArr[$i],'private');
        }
        return $tempArray;
      }
    }

    public function getCPictureArrAttribute(){
      if(array_key_exists('c_picture',$this->attributes)){
        return json_decode($this->attributes['c_picture']);
      }
    }

   	public function commodityType(){
   		return $this->belongsTo('App\Models\CommodityType','ct_type_id');
   	}
}
